﻿using System;

using Autofac;

using Cookbook.Services.Core.Domain;
using Cookbook.Services.Core.Persistency;

using Cooking.Services.Specs.Support;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cooking.Services.Specs.Persistency
{
    [TestClass]
    public class RecipeRepositorySpecs
    {
        [TestMethod]
        public void When_creating_a_recipe_with_a_duplicate_name_it_should_throw_a_rule_violation()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var containerBuilder = new ContainerBuilder();
            containerBuilder.RegisterModule<SqlLiteUnitOfWorkModule>();
            var container = containerBuilder.Build();

            using (var uow = container.Resolve<Func<CookbookUnitOfWork>>()())
            {
                uow.Recipes.Add(new Recipe("Macaroni", "yummi"));
                uow.SubmitChanges();

                //-----------------------------------------------------------------------------------------------------------
                // Act
                //-----------------------------------------------------------------------------------------------------------
                Action action = () => uow.Recipes.Add(new Recipe("Macaroni", "Also yummi"));

                //-----------------------------------------------------------------------------------------------------------
                // Assert
                //-----------------------------------------------------------------------------------------------------------
                action.ShouldViolate(CookbookRule.RecipeTitleMustBeUnique);
            }
        }
        
        [TestMethod]
        public void When_creating_a_recipe_with_a_name_that_is_too_long_it_should_throw_a_rule_violation()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var containerBuilder = new ContainerBuilder();
            containerBuilder.RegisterModule(new CookbookUnitOfWorkModule("Cookbook"));
            var container = containerBuilder.Build();

            using (var uow = container.Resolve<Func<CookbookUnitOfWork>>()())
            {
                //-----------------------------------------------------------------------------------------------------------
                // Act
                //-----------------------------------------------------------------------------------------------------------
                Action action = () => uow.Recipes.Add(new Recipe(new string('a', 10000), "yummi"));

                //-----------------------------------------------------------------------------------------------------------
                // Assert
                //-----------------------------------------------------------------------------------------------------------
                action.ShouldViolate(CookbookRule.RecipeTitleMustBeShort);
            }
        }
    }
}